package com.zlx.sharelive.activity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.zlx.sharelive.R;
import com.zlx.sharelive.adapter.AreaCodeAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.adapter.SimpleRecyclerViewAdapter;
import com.zlx.sharelive.bean.AreaCode;
import com.zlx.sharelive.bean.AreaCodeItem;
import com.zlx.sharelive.view.AreaCodeDividerDecoration;
import com.zlx.sharelive.view.AreaCodeIndexDecoration;
import com.zlx.sharelive.view.DataSource;
import com.zlx.sharelive.view.IndexView;

import java.util.ArrayList;
import java.util.List;

public class AreaCodeListAc extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private List<AreaCodeItem> mCodeList;
    private List<AreaCode> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<AreaCode> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_area_code_list);

        setListData();

        mRecyclerView = findViewById(R.id.recycler_view);
        IndexView indexView = findViewById(R.id.index_view);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        RecyclerViewHeader header = RecyclerViewHeader.fromXml(this, R.layout.area_code_header);
        header.attachTo(mRecyclerView);

        RecyclerView rv_data = header.findViewById(R.id.rv_data);

        rv_data.setLayoutManager(new LinearLayoutManager(this));

        mAdapter = new CommonRecyclerViewAdapter<AreaCode>(this, mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, AreaCode entity, int position) {
                holder.setText(R.id.text_area, entity.getArea());
                holder.setText(R.id.text_code, entity.getCode());
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_area_code;
            }
        };

        rv_data.setAdapter(mAdapter);

        mAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Intent intent = new Intent();
                intent.putExtra("code", mList.get(position).getCode());
                setResult(30, intent);
                finish();
            }
        });

        AreaCodeIndexDecoration groupDecoration = new AreaCodeIndexDecoration(100, 0xffF1F4F6, 32, 0xff666666);
        //实现index view显示
        mRecyclerView.addItemDecoration(groupDecoration);
        //实现分割线显示
        mRecyclerView.addItemDecoration(new AreaCodeDividerDecoration(new ColorDrawable(ContextCompat.getColor(this, R.color.colorAccent)), 24, 24));

        List<AreaCode> codeList = DataSource.getAreaCodeList(this);
        indexView.setIndexList(DataSource.getIndexList(codeList));

        AreaCodeAdapter areaCodeAdapter = new AreaCodeAdapter();
        mCodeList = DataSource.getAreaCodeItemList(codeList);
        areaCodeAdapter.updateData(mCodeList);
        mRecyclerView.setAdapter(areaCodeAdapter);

        areaCodeAdapter.setItemClickListener(new SimpleRecyclerViewAdapter.OnItemClickListener<AreaCodeItem>() {
            @Override
            public void onItemClick(int position, AreaCodeItem item) {
                Log.d("ret", "joker   选择的+" + codeList.get(position).getCode());
                Intent intent = new Intent();
                intent.putExtra("code", codeList.get(position).getCode());
                setResult(30, intent);
                finish();
            }
        });

        indexView.setOnSelectedListener(new IndexView.OnSelectedListener() {
            @Override
            public void onSelected(String index) {
                selectedIndex(index);
            }
        });

        ImageView iv_back = findViewById(R.id.iv_back);

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void selectedIndex(String index) {
        int position = -1;
        for (int i = 0; i < mCodeList.size(); i++) {
            if (mCodeList.get(i).getGroupName().equals(index)) {
                position = i;
                break;
            }
        }

        if (-1 == position) {
            return;
        }
        mRecyclerView.smoothScrollToPosition(position);
    }

    private void setListData() {
        mList.clear();
        AreaCode areaCode1 = new AreaCode("中国", "+86");
        AreaCode areaCode2 = new AreaCode("中国香港", "+886");
        AreaCode areaCode3 = new AreaCode("中国澳门", "+882");
        AreaCode areaCode4 = new AreaCode("中国台湾", "+863");

        mList.add(areaCode1);
        mList.add(areaCode2);
        mList.add(areaCode3);
        mList.add(areaCode4);
    }

}
